package com.ruoyi.web.plan.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.framework.utils.DateUtils;
import com.ruoyi.framework.utils.StringUtils;
import com.ruoyi.framework.utils.ip.AddressUtils;
import com.ruoyi.web.plan.domain.Project;
import com.ruoyi.web.plan.mapper.ProjectMapper;
import com.ruoyi.web.plan.service.IProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-30
 */
@Service
public class ProjectServiceImpl implements IProjectService {

    private static final Logger log = LoggerFactory.getLogger(ProjectServiceImpl.class);

    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 查询项目信息
     *
     * @param projectId 项目信息主键
     * @return 项目信息
     */
    @Override
    public Project selectProjectByProjectId(Long projectId) {
        Project project = projectMapper.selectProjectByProjectId(projectId);
        if (project.getPeopleIds() != null && !project.getPeopleIds().equalsIgnoreCase("")) {
            List<Long> personList = JSON.parseArray(project.getPeopleIds(), Long.class);
            for (Long aLong : personList) {
                System.out.println(aLong + "====");
            }
            project.setUserIdList(personList);
        }
        return project;
    }

    /**
     * 查询项目信息列表
     *
     * @param project 项目信息
     * @return 项目信息
     */
    @Override
    public List<Project> selectProjectList(Project project) {
        List<Project> projectList = projectMapper.selectProjectList(project);
        for (Project item : projectList) {
            System.out.println(item.getPeopleIds() + "====");
            if (item.getPeopleIds() != null && !item.getPeopleIds().equalsIgnoreCase("")) {
                List<Long> personList = JSON.parseArray(item.getPeopleIds(), Long.class);
                for (Long aLong : personList) {
                    System.out.println(aLong + "====");
                }
                item.setUserIdList(personList);
            }
        }
        return projectList;
    }

    /**
     * 新增项目信息
     *
     * @param project 项目信息
     * @return 结果
     */
    @Override
    public int insertProject(Project project) {
        project.setCreateTime(DateUtils.getNowDate());
        return projectMapper.insertProject(project);
    }

    /**
     * 修改项目信息
     *
     * @param project 项目信息
     * @return 结果
     */
    @Override
    public int updateProject(Project project) {
        project.setUpdateTime(DateUtils.getNowDate());
        Project sqlPro = projectMapper.selectProjectByProjectId(project.getProjectId());
        if (sqlPro != null && sqlPro.getStartTime() != null) {
            project.setStartTime(null);
        }
        return projectMapper.updateProject(project);
    }

    /**
     * 批量删除项目信息
     *
     * @param projectIds 需要删除的项目信息主键
     * @return 结果
     */
    @Override
    public int deleteProjectByProjectIds(Long[] projectIds) {
        return projectMapper.deleteProjectByProjectIds(projectIds);
    }

    /**
     * 删除项目信息信息
     *
     * @param projectId 项目信息主键
     * @return 结果
     */
    @Override
    public int deleteProjectByProjectId(Long projectId) {
        return projectMapper.deleteProjectByProjectId(projectId);
    }
}
